package com.iseastar.guojiang.order;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.cabinet.CabinetParcelsListActivity;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;

import java.io.Serializable;

import droid.frame.activity.title.TitleRes;
import droid.frame.ui.dialog.AppDialog;

/**
 * 柜子、拒绝接单
 *
 * @author coffee <br>
 *         2016-10-8下午5:04:36
 */
public class OrderRefuseActivity extends BaseActivity2 implements OnClickListener {
	// 拒收原因
	private EditText mReason;

	// 订单id、或者包裹id
	private Serializable itemId;
	private final int REFUSE_SIZE = 4;
	private RadioButton[] mRefuseItems;// 拒收

	private RadioButton selectedRadio = null;

	@Override
	@SuppressLint("ResourceAsColor")
	protected void onCreate(Bundle savedInstanceState) {
		itemId = getIntent().getSerializableExtra("itemId");
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void findViewById() {
		setContentView(R.layout.order_refuse);
		super.findViewById();
		getAppTitle().setCommonTitle("拒收", new TitleRes("提交", new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (selectedRadio == null) {
					showToast("请输入拒收原因");
					return;
				}
				int checkedId = selectedRadio.getId();
				// 选中的是"其他" 但是未填写原因
				if (checkedId == mRefuseItems[REFUSE_SIZE - 1].getId() && isEmpty(mReason.getText())) {
					showToast("请输入拒收原因");
					return;
				}
				AppDialog dialog = new AppDialog(getContext());
				dialog.setContent("确定拒收这个包裹?");
				dialog.setCancelClickListener("取消", null);
				dialog.setConfirmClickListener("确定", new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						onRefuse();
					}
				});
				dialog.show();
			}
		}));
		mReason = (EditText) findViewById(R.id.order_reason);
		mRefuseItems = new RadioButton[REFUSE_SIZE];
		for (int i = 0; i < REFUSE_SIZE; i++) {
			int resid = getResources().getIdentifier("order_item" + (i + 1), "id", getPackageName());
			mRefuseItems[i] = (RadioButton) findViewById(resid);
			mRefuseItems[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						if (selectedRadio != null) {
							selectedRadio.setChecked(false);// 把之前的选中项置为非选中
						}
						selectedRadio = (RadioButton) buttonView;
						// 其他
						if (buttonView.getId() == mRefuseItems[REFUSE_SIZE - 1].getId()) {
							mReason.setVisibility(View.VISIBLE);
						} else {
							mReason.setVisibility(View.INVISIBLE);
						}
					}
				}
			});
		}
	}

	private void onRefuse() {
		String reason = selectedRadio.getText().toString();
		// 选的是其他
		int checkedId = selectedRadio.getId();
		if (checkedId == mRefuseItems[REFUSE_SIZE - 1].getId()) {
			reason = mReason.getText().toString();
		}
		showLoadingDialog(null);
		String stationPhone = AppCache.getCabinetOrStation().getPhone();
		if (isEmpty(stationPhone)) {// 柜子拒收
			AppHttp.getInstance().orderReject(itemId, reason);
		} else {// 驿站拒收
			AppHttp.getInstance().orderReject2(itemId, reason, stationPhone);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_right_switcher:
				// 未选中

				break;
			default:
				break;
		}
	}

	/**
	 * {@link CabinetParcelsListActivity#handleMessage(Message)}
	 */
	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
			// 拒收
			case MsgID.order_refuse:
				cancelLoadingDialog();
				ReqResult<String> result = JSON.parser(msg.obj, String.class);
				if (checkLoginStatus(result)) {
					String code = result.getResult();// 开箱码
					Intent data = new Intent();
					data.putExtra("code", code);
					setResult(RESULT_OK, data);
					finish();
				} else {
					showToast(result.getMessage());
				}
				return false;// 列表界面也需要
			default:
				return super.handleMessage(msg);
		}
	}
}
